﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EducationManagementSystem.Admin
{
    public partial class ZhuRendetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
                Bind2();
            }
        }

        /// <summary>
        /// 学院绑定
        /// </summary>
        private void Bind()
        {

            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select * from school";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader ds = cmd.ExecuteReader();
            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "schoolname";
            DropDownList1.DataValueField = "schoolID";
            DropDownList1.DataBind();
        }
        /// <summary>
        ///第一次页面加载时 判断传过来是什么请求
        /// </summary>
        protected void Bind2()
        {
            if (Request.QueryString["Flag"] == "Add")
            {
                //清空控件
                this.TextBox1.Text = "";
                this.TextBox2.Text = "";
            }
            else
            {
                TextBox1.Enabled = false;
                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "select a.zhurenid,a.zhurenname,b.schoolname,b.schoolid  from zhuren a,school b where zhurenid='" + Request.QueryString["ID"] + "' and a.schoolid=b.schoolid";
                SqlCommand cmd = new SqlCommand(sql, conn);
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(ds);
                TextBox1.Text = ds.Tables[0].Rows[0][0].ToString();
                TextBox2.Text = ds.Tables[0].Rows[0][1].ToString();
                DropDownList1.SelectedValue = ds.Tables[0].Rows[0][3].ToString();
            }
        }
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["Flag"] == "Add")
            {
                //判断信息是否正确
                if (this.TextBox1.Text == "")
                {
                    this.Terr.InnerHtml = "<font color='red'>帐号不能为空</font>";
                    return;
                }
                else
                {
                    SqlConnection conn = DB.Conn();
                    conn.Open();
                    string sql = "select * from zhuren where zhurenid='" + TextBox1.Text + "'";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    object a = cmd.ExecuteScalar();
                    if (a != null)
                    {
                        this.Page.RegisterStartupScript(" ", "<script>alert('账号重复，请重新设置'); </script> ");
                        TextBox1.Text = "";
                    }
                    else
                    {
                        string sql2 = "insert into zhuren(zhurenid,zhurenname,schoolid,zhurenpwd) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedValue + "','000000')";
                        cmd = new SqlCommand(sql2, conn);
                        int c = cmd.ExecuteNonQuery();
                        if (c > 0)
                        {
                            this.Page.RegisterStartupScript(" ", "<script>alert('添加成功'); </script> ");
                            Response.Redirect("ZhuRenManage.aspx");
                        }
                        else
                        {
                            this.Page.RegisterStartupScript(" ", "<script>alert('添加失败'); </script> ");
                        }
                    }
                }
            }
            else
            {

                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "update zhuren set zhurenname='" + TextBox2.Text + "',schoolid='" + DropDownList1.SelectedValue + "'  where zhurenid='" + Request.QueryString["ID"] + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                int a = cmd.ExecuteNonQuery();
                if (a > 0)
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert('修改成功'); </script> ");
                    Response.Redirect("ZhuRenManage.aspx");
                }
                else
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert('修改失败'); </script> ");
                }

            }
        }
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("ZhuRenManage.aspx");
        }
    }
}